package cn.trda.base.refject;

import java.lang.reflect.InvocationTargetException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.converters.DateConverter;
import org.apache.commons.beanutils.locale.converters.DateLocaleConverter;
import org.junit.Test;

import cn.trda.pojo.Person;

class MyConverter implements Converter{

	@Override
	public Object convert(Class arg0, Object value) {//1988aaaa   
		// TODO Auto-generated method stub
		if(value==null){
			return null;
		}
		String result=(String) value;
		if(result.trim().isEmpty()){
			return null;
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		
		
		try {
			return sdf.parse(result);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
		
			throw new RuntimeException(e);
		}
	}

	
	
}

public class BeanUtilDemo {
	@Test
	public void testAdd() throws Exception{
			Person p=new Person();
		//用beanutils这个工具类实现操作
		BeanUtils.copyProperty(p, "username", "admin");
		BeanUtils.copyProperty(p, "age", 15);
		BeanUtils.copyProperty(p, "height", 173.5);
		
		System.out.println(p);
	}
	@Test
	public void test22() throws Exception{
		/**
		 *   用户名：————————     request.getPramete("");   p.setUser
		 *   年龄 ：——————————    request.getPramete("");   p.setAge
		 *   身高：——————————     request.getPramete("");  p.setHeight();
		 *   生日：——1988-8-8—     request.getPramete(""); 
		 *   			Map		request.getParmeteMaper();
		 * 				《提交》
		 */
		//用beanutils这个工具类实现操作 响应速度  开机速度 // 解决方案＝＝技术＝＝工具＝＝框架frame work
		Map<String,String> map=new HashMap<>();
		map.put("username", "admin");
		map.put("age", "222");
		map.put("height", "175.9");
		map.put("birthday", "    ");
		Person p=new Person();
		
		ConvertUtils.register(new Converter() {
			//
			@Override
			public Object convert(Class arg0, Object value) {
				if(value==null){
					return null;
				}
				String result=(String) value;
				if(result.trim().isEmpty()){
					return null;
				}
				SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
				
				
				try {
					return sdf.parse(result);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
				
					throw new RuntimeException(e);
				}
			}
		}, Date.class);
		BeanUtils.populate(p, map);
		System.out.println(p);
	}
}


